package com.tl.boot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tl.boot.common.annotation.ChooseDataSource;
import com.tl.boot.common.annotation.RestControllerRequstMapping;
import com.tl.boot.common.enums.LocalDataBaseEnum;
import com.tl.boot.common.vo.PageInfoVO;
import com.tl.boot.common.vo.ResponseVO;
import com.tl.boot.entity.demo.dto.DemoInfoDTO;
import com.tl.boot.entity.demo.form.DemoInfoListForm;
import com.tl.boot.entity.demo.vo.DemoInfoVO;
import com.tl.boot.service.demo.DemoService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


/**
 * com.tl.boot.controller -- DemoInfoController
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/10 11:54
 */
@Api(tags = "demo")
@RestControllerRequstMapping("demo")
public class DemoInfoController {
    @Autowired
    private DemoService demoService;

    @GetMapping("01")
    public ResponseVO demo() {
        return ResponseVO.buildSuccess("hello");
    }

    @PostMapping("list")
    @ChooseDataSource(LocalDataBaseEnum.SECONDARY_DRUID)
    public ResponseVO<PageInfoVO<DemoInfoVO>> list(@RequestBody DemoInfoListForm form) {
        PageHelper.startPage(form.getPage(), form.getSize());
        DemoInfoDTO demoInfoDTO = new DemoInfoDTO();
        BeanUtils.copyProperties(form, demoInfoDTO);
        List<DemoInfoVO> list = demoService.list(demoInfoDTO);
        Page<DemoInfoVO> page = (Page<DemoInfoVO>) list;
        return ResponseVO.buildSuccess(page.getResult(),page.getTotal());
    }
}
